package com.liferay.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.liferay.dao.ConnectDao;
import com.liferay.dao.YearOfSemesterDao;
import com.liferay.model.YearOfSemester;

public class YearOfSemesterDaoImpl implements YearOfSemesterDao {
	private static final String GET_MAX_SEMESTER = "SELECT MAX(id) as id,MAX(semester) as semes,yearOfSemester FROM nlu_yearofsemester WHERE yearOfSemester = (SELECT yearOfSemester FROM nlu_yearofsemester ORDER BY yearOfSemester DESC LIMIT 1);";
	private static final String INSERT_SEMESTER = "INSERT INTO nlu_yearofsemester VALUES (?,?,?);";
	private static final String GET_ID_SEMESTER = "SELECT * FROM nlu_yearofsemester as a WHERE yearOfSemester = (SELECT yearOfSemester FROM nlu_yearofsemester ORDER BY yearOfSemester DESC LIMIT 1) AND id=?;";
	private static final String DELETE_SEMESTER_BY_ID = "DELETE FROM nlu_yearofsemester WHERE id=?";
	private PreparedStatement pre;
	private ResultSet rs;
	private Connection conn;
	private List<YearOfSemester> listSemester;
	private YearOfSemester semester;

	@Override
	public List<YearOfSemester> getMaxSemester() {
		listSemester = new ArrayList<YearOfSemester>();
		conn = ConnectDao.getConnection();
		try {
			semester = new YearOfSemester();
			pre = conn.prepareStatement(GET_MAX_SEMESTER);
			rs = pre.executeQuery();
			while (rs.next()) {
				semester.setId(rs.getLong("id"));
				semester.setSemester(rs.getInt("semes"));
				semester.setYearOfSemester(rs.getString("yearOfSemester"));
				listSemester.add(semester);
			}
			rs.close();
			pre.close();
			ConnectDao.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listSemester;
	}

	@Override
	public boolean insertSemester(YearOfSemester yearOfSemester) {
		conn = ConnectDao.getConnection();
		try {
			pre = conn.prepareStatement(INSERT_SEMESTER);
			pre.setLong(1, yearOfSemester.getId());
			pre.setInt(2, yearOfSemester.getSemester());
			pre.setString(3, yearOfSemester.getYearOfSemester());
			pre.executeUpdate();
			pre.close();
			ConnectDao.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String checkStringToken(String yearOfSemes) {
		StringTokenizer token = new StringTokenizer(yearOfSemes, "-");
		if (token.hasMoreTokens()) {
			String yearStart = token.nextToken();
			String yearEnd = token.nextToken();
			int year1 = Integer.parseInt(yearStart);
			int year2 = Integer.parseInt(yearEnd);
			year1 += 1;
			year2 += 1;
			String yearOne = Integer.toString(year1);
			String yearTwo = Integer.toString(year2);
			String yearTotal = yearOne + "-" + yearTwo;
			return yearTotal;
		} else {
			return null;
		}
	}

	public static void main(String[] args) {
		YearOfSemesterDaoImpl sub = new YearOfSemesterDaoImpl();
		System.out.println(sub.getMaxSemester());
		// YearOfSemester year = new YearOfSemester();
		// System.out.println(sub.insertSemester(new
		// YearOfSemester(year.getId(),
		// 1, sub.checkStringToken("2013-2014"))));
		// System.out.println(sub.getListYearOfSemester(6));
		System.out.println(sub.deleteSemester(2));
	}

	@Override
	public List<YearOfSemester> getListYearOfSemester(long id) {
		conn = ConnectDao.getConnection();
		List<YearOfSemester> listYear = new ArrayList<YearOfSemester>();
		try {
			pre = conn.prepareStatement(GET_ID_SEMESTER);
			pre.setLong(1, id);
			rs = pre.executeQuery();
			while (rs.next()) {
				YearOfSemester year = new YearOfSemester();
				year.setId(rs.getLong("id"));
				year.setSemester(rs.getInt("semester"));
				year.setYearOfSemester(rs.getString("yearOfSemester"));
				listYear.add(year);
			}
			pre.close();
			rs.close();
			ConnectDao.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listYear;
	}

	@Override
	public boolean deleteSemester(long id) {
		conn = ConnectDao.getConnection();
		try {
			pre = conn.prepareStatement(DELETE_SEMESTER_BY_ID);
			pre.setLong(1, id);
			pre.executeUpdate();
			pre.close();
			ConnectDao.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
